<?php
define('IN_ECS', true);

require(dirname(dirname(__FILE__)) . '/includes/init.php');

$sql = 'SELECT * from tage_integral_goods';

$goods = $db->getAll($sql);

$action=$_GET["act"];
$cgid = $_GET["id"];

if($action=='add')
{
	$sqlid = 'SELECT goods_sn,goods_name,market_price from tage_goods where is_delete = 0';
	$stores = 'SELECT StoreID,StoreName from tage_stores where ProvinceID = "2" and Status = 1 order by DistrictID asc';
	$sqlpartner = 'SELECT cid,cname from tage_integral_com';
	$partner = $db->getAll($sqlpartner);

    $stoRes = $db -> getAll($stores);
	$goodslist = $db->getAll($sqlid);

    $smarty->assign('citylist',$stoRes);
    $smarty->assign('cgid',"0");
  
    $smarty->assign('partner',$partner);
    $smarty->assign('goodslist',$goodslist);
    $smarty->display('jfactivity_add.dwt');
}
elseif ($action=="save")
{
	$goodsid=$_POST['goodsid'];
    $integralvalue=$_POST['integralvalue'];
    $price=$_POST['price'];
    $saleprice=$_POST['saleprice'];
    $begintime=strtotime($_POST['begintime']);
    $endtime=strtotime($_POST['endtime']);
    $CID=$_POST['CID'];
    $STOREID=$_POST['selectstroe'];
    $boundprice=$_POST['boundprice'];
    $typ=$_POST['typ'];
    $expiredays=$_POST['expiredays'];
    $cgid=$_POST['cgid'];
	if ($typ==1)      //判断是红包还是商品
	{
		$goodsid="hb001";
	}
	$sql = "";
    if ($cgid==0)
    {
    	$sql="select * from tage_integral_goods where cid=".$CID." and goodsid='".$goodsid."'";
        $query=mysql_query($sql);
        $rows = mysql_num_rows($query);
        // echo $rows;
        // var_dump($rows);
        if($rows>0&&$typ==0)
        {
 	    echo "<script type='text/javascript'>alert('商品已存在');location='javascript:history.back()';</script>";
	    return;
        }
	    
        else
        {
        	 $sql="insert into tage_integral_goods(goodsid,integralvalue,price,saleprice,begintime,endtime,CID,STOREID,boundprice,typ,expiredays)values('$goodsid','$integralvalue','$price','$saleprice','$begintime','$endtime','$CID','$STOREID','$boundprice','$typ','$expiredays')";
        }
	//var_dump($sql);
    }
    else
    {
    	$sql=" UPDATE tage_integral_goods SET goodsid='$goodsid',integralvalue='$integralvalue',price='$price',saleprice='$saleprice',begintime='$begintime',endtime='$endtime',CID='$CID',STOREID='$STOREID',boundprice='$boundprice',typ='$typ',expiredays='$expiredays' where cgid=".$cgid;
    }

	$query=mysql_query($sql);

	$sql = 'SELECT * from tage_integral_goods';
	$goods = $db->getAll($sql);
	$smarty->assign('goods',$goods);

    $smarty->display('jfactivity.dwt');
	
}
elseif ($action=="del")
{
    $rowID=$_GET["id"];
    $query="delete from tage_integral_goods where cgid='$rowID'";//查询语句
    $result=mysql_query($query);//放到数据库执行
    
    if(!mysql_affected_rows()==0){
    
    }
    
    if(mysql_affected_rows()==0){
  
    }
   	$sql = 'SELECT * from tage_integral_goods';
	$goods = $db->getAll($sql);
	$smarty->assign('goods',$goods);

    $smarty->display('jfactivity.dwt');
}
elseif ($action=="list")
{
		$sql = 'SELECT a.*,date_format( from_unixtime(begintime),'%y-%m-%d') as begintime,date_format( from_unixtime(endtime),'%y-%m-%d') as endtime,b.goods_name from tage_integral_goods as a ,tage_goods as b where a.goodsid = b.goods_sn';
        // $sqlcname = 'SELECT b.cname FROM tage_integral_goods as a, tage_integral_com as b WHERE a.cid = b.cid';
        // $sqlbegintime= 'SELECT FROM_UNIXTIME(begintime) from  tage_integral_goods';
        // $sqlendtime= 'SELECT FROM_UNIXTIME(endtime) from  tage_integral_goods';

	    $goods = $db->getAll($sql);
        // $cname = $db->getAll($sqlcname);
        // $begintime = $db->getAll($sqlbegintime);
        // $endtime = $db->getAll($sqlendtime);
        // var_dump($cname);
        // var_dump($begintime);
        // var_dump($endtime);
       
     

	    $smarty->assign('goods',$goods);

        // $smarty->assign('cname',$cname);
        $smarty->display('jfactivity.dwt');
   
}
elseif($action=="edit")
{
	$sql = "select * from tage_integral_goods where cgid=".$cgid;
	$goods= $db->getRow($sql);

	if ($goods)
	{
		$goodsid = $goods['goodsid'];
		$boundprice=$goods['boundprice'];
		$integralvalue=$goods['integralvalue'];
        $price=$goods['price'];
        $saleprice=$goods['saleprice'];
        $begintime=date('Y-m-d H:i:s', $goods['begintime']);
        $endtime=date('Y-m-d H:i:s',$goods['endtime']);
        $CID=$goods['cid'];
        $STOREID=$goods['storeid'];
        $typ=$goods['typ'];
        $expiredays=$goods['expiredays'];

		$smarty->assign('cgid',$cgid);

	    $sqlid = 'SELECT goods_sn,goods_name,market_price from tage_goods where is_delete = 0';
	    
	   
	    $stoRes = array();
	    $stoRes1  = array();
	    if(empty($STOREID))   //判断商品是否有门店
	    {
	       $sql1 = 'SELECT StoreID,StoreName from tage_stores where  ProvinceID = "2" and Status = 1 order by DistrictID asc';
	      // echo $sql1;
	        // return;
	       $stoRes = $db -> getAll($sql1);
	    }
	    else
	    {
	    $sql1 = 'SELECT StoreID,StoreName from tage_stores where StoreID not in ('.$STOREID.') and ProvinceID = "2" and Status = 1 order by DistrictID asc';
	    $stoRes = $db -> getAll($sql1);
	
        $sql2 = 'SELECT StoreID,StoreName from tage_stores where StoreID in ('.$STOREID.') and ProvinceID = "2" and Status = 1 order by DistrictID asc';
	    $stoRes1 = $db -> getAll($sql2);
	    }

	    $sqlpartner = 'SELECT cid,cname from tage_integral_com';
	    $partner = $db->getAll($sqlpartner);
	    $goodslist = $db->getAll($sqlid);

        $smarty->assign('goodsid',$goodsid);
        $smarty->assign('boundprice',$boundprice);
        $smarty->assign('integralvalue',$integralvalue);
        $smarty->assign('price',$price);
        $smarty->assign('saleprice',$saleprice);
        $smarty->assign('begintime',$begintime);
        $smarty->assign('endtime',$endtime);
        $smarty->assign('CID',$CID);
        $smarty->assign('STOREID',$STOREID);
        $smarty->assign('typ',$typ);
        $smarty->assign('expiredays',$expiredays);


        $smarty->assign('citylist',$stoRes);
        $smarty->assign('citylist2',$stoRes1);
        $smarty->assign('partner',$partner);
        $smarty->assign('goodslist',$goodslist);

        $smarty->display('jfactivity_add.dwt');
		
	}
}


?>